package com.service.business.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
import com.service.business.order.dto.OrderProductOtherTotalVo;
import com.service.business.order.service.OrderProductOtherTotalService;
import com.service.dao.model.OrderProductOtherTotal;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.service.dao.mapper.OrderProductOtherTotalMapper;
import org.springframework.transaction.annotation.Transactional;


@Service("orderProductOtherTotalService")
public class OrderProductOtherTotalServiceImpl extends ServiceImpl<OrderProductOtherTotalMapper, OrderProductOtherTotal> implements OrderProductOtherTotalService {


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createdOrderTotal(OrderProductOtherTotalVo orderProductOtherTotalVo) {

        Map<Integer, BigDecimal> priceMap = orderProductOtherTotalVo.getTotalPriceMapByProductType();
        Date current = new Date();
        List<OrderProductOtherTotal> totalList = priceMap.keySet().stream().map(item -> OrderProductOtherTotal.builder()
                .totalPrice(priceMap.get(item))
                .totalPriceCoin(orderProductOtherTotalVo.getPayCoinsByProductType().get(item).getName())
                .totalGift(orderProductOtherTotalVo.getTotalGiftMapByProductType().get(item))
                .totalGiftCoin(orderProductOtherTotalVo.getGiftCoinsByProductType().get(item).getName())
                .totalFreight(orderProductOtherTotalVo.getTotalFreightByProductType().get(item))
                .totalFreightCoin(orderProductOtherTotalVo.getFreightCoins().getName())
                .orderId(orderProductOtherTotalVo.getOrderId())
                .productType(item)
                .createTime(current)
                .build()).collect(Collectors.toList());

        saveBatch(totalList);
    }

    @Override
    public List<OrderProductOtherTotal> listTotalByOrderId(Integer orderId) {
        return list(new LambdaQueryWrapper<OrderProductOtherTotal>().eq(OrderProductOtherTotal::getOrderId, orderId));
    }

    @Override
    public Map<String, BigDecimal> listTotal4PayByOrderId2Map(Integer orderId) {

        List<OrderProductOtherTotal> resultList = listTotalByOrderId(orderId);
        Map<String, BigDecimal> resultMap = Maps.newHashMap();
        for (OrderProductOtherTotal item : resultList){
            resultMap.put(item.getTotalPriceCoin(), resultMap.getOrDefault(item.getTotalPriceCoin(), BigDecimal.ZERO).add(item.getTotalPrice()));
            resultMap.put(item.getTotalFreightCoin(), resultMap.getOrDefault(item.getTotalFreightCoin(), BigDecimal.ZERO).add(item.getTotalFreight()));
        }

        return resultMap;
    }

    @Override
    public Map<String, BigDecimal> listTotalByOrderId2Map(Integer orderId) {
        return listTotalByOrderId2Map(listTotalByOrderId(orderId));
    }

    @Override
    public Map<String, BigDecimal> listTotalByOrderId2Map(List<OrderProductOtherTotal> totalList) {
        Map<String, BigDecimal> resultMap = Maps.newHashMap();
        for (OrderProductOtherTotal item : totalList){
            resultMap.put(item.getTotalPriceCoin(), resultMap.getOrDefault(item.getTotalPriceCoin(), BigDecimal.ZERO).add(item.getTotalPrice()));
            resultMap.put(item.getTotalFreightCoin(), resultMap.getOrDefault(item.getTotalFreightCoin(), BigDecimal.ZERO).add(item.getTotalFreight()));
            resultMap.put(item.getTotalGiftCoin(), resultMap.getOrDefault(item.getTotalGiftCoin(), BigDecimal.ZERO).add(item.getTotalGift()));
        }
        return resultMap;
    }
}
